package com.eostoken.eos_wallet.ui.adapter;

import androidx.core.content.ContextCompat;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.JsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.bean.BillBean;
import com.eostoken.eos_wallet.bean.TransferDataBean;
import com.eostoken.eos_wallet.utils.TimeUtils;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;

public class BillAdapter extends BaseQuickAdapter<BillBean.ActionsBean, BaseViewHolder> implements LoadMoreModule {

    private TimeUtils timeUtils;
    private String currentAccount = "";

    public BillAdapter(int layoutResId, String currentAccount) {
        super(layoutResId);
        this.currentAccount = currentAccount;
        timeUtils = new TimeUtils();
        LogUtils.dTag("BillAdapter==", " currentAccount = " + currentAccount);
    }

    /**
     * 1、action_trace-->receipt-->receiver 获取receiver值，判断是否是等于当前用户名，是则表示为当前用户名下的交易记录，反之不是
     * 2、action_trace-->act-->data-->to 获取to值，判断是否为当前用户名，是则表示转入，反之为转出
     */
    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, BillBean.ActionsBean actionsBean) {
        BillBean.ActionsBean.ActionTraceBean actionTraceBean = actionsBean.getAction_trace();
        BillBean.ActionsBean.ActionTraceBean.ActBean actBean = actionTraceBean.getAct();
        try {
            TransferDataBean dataBean = GsonUtils.fromJson(GsonUtils.toJson(actBean.getData()), new TypeToken<TransferDataBean>() {
            }.getType());

            String to = dataBean.getTo();
            String from = dataBean.getFrom();
            LogUtils.dTag("BillAdapter==", " from = " + from);
            String quantity = dataBean.getQuantity();
            String name = actBean.getName();
            String block_time = actionTraceBean.getBlock_time();
            String trx_id = actionTraceBean.getTrx_id();
            int res = -1;
            int color = R.color.app_color_blue;
            boolean isOut = false;
            String account;
            if (ObjectUtils.equals(currentAccount, to)) {
                res = R.mipmap.into_icon;
                color = R.color.app_color_blue;
                isOut = false;
                account = from;
            } else {
                res = R.mipmap.out_icon;
                color = R.color.app_color_dark_blue;
                isOut = true;
                account = to;
            }
            baseViewHolder.setImageResource(R.id.iconIv, res);
            baseViewHolder.setText(R.id.accountsTv, account);
            baseViewHolder.setText(R.id.timeTv, timeUtils.getStringTime(block_time));
            baseViewHolder.setText(R.id.amountTv, (isOut ? "-" : "+") + quantity)
                    .setTextColor(R.id.amountTv, ContextCompat.getColor(getContext(), color));
            baseViewHolder.setText(R.id.typeTv, name);
            baseViewHolder.itemView.setTag(R.id.OrderID, trx_id);
            baseViewHolder.itemView.setTag(R.id.quantity, quantity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
